<template>
  <div>
    <div class="node" v-has="{action: 'sys:role:list', effect: 'move'}">
    <!-- <div :class="[$style.search_box, $style.topSelect]"> -->
    <div :class="[$style.search_box, $style.topSelect]">
      <SearchInput
        placeholder="角色名称"
        label="角色名称"
        style="margin-right:20px"
        :value="name"
        :onChange="(val) => changeSearch('name',val)"
      />
      <div class="searchBox1" style="margin-left: 20px">
        <el-button size="small" plain icon="el-icon-search" @click="queryData">查询</el-button>        
      </div>
    </div>
    <Panel title="角色管理">
      <div slot='ext' :class="$style.examine">
        <!-- <el-button type="default" icon="el-icon-refresh" @click="queryData">刷新</el-button> -->
        <el-button class="button-offt" v-has="{action: 'sys:role:add', effect: 'move'}" type="success" icon="el-icon-plus" @click="()=>edit()">新增</el-button>
    </div>
    <TableEncapsulation
      slot="body"
      :tableConfig="tableConfig"
      :tableData="tableData"
    >
    <template slot="operation" slot-scope="operation">
          <el-button size="mini" icon="el-icon-edit"
            v-has="{action: 'sys:role:update', effect: 'move'}"
            @click="()=>edit(operation.row)"
          >编辑</el-button>
           <el-button size="mini" icon="el-icon-setting"
            v-has="{action: 'sys:role:list', effect: 'move'}"
            @click="()=>editStore(operation.row)"
          >分配权限</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete"
            v-has="{action: 'sys:role:delete', effect: 'move'}"
            @click="()=>remove(operation.row.id)"
          >删除</el-button>
        </template>
        <TablePager
          slot="pager"
          v-if="pager.use"
          :size="pager.size"
          :pageChange="pageChange"
          :show-total="pager.showTotal"
          :total="pager.total"
          :current="pager.current"
        >
          <span class="demonstration">GO></span>
        </TablePager>
    </TableEncapsulation>
    </Panel>
    <ModalPanel 
      :visible='visible'
      title="编辑角色"
      :onCancel="onCancel"
      :class="$style.bannerDialog"
      :onOk="onSubmit">
      <div>
        <ModifyForm
          :hiddenBtn='true'
          :editColumn='editColumn'
          :updateData="updateData"
          labelWidth='100px'
          ref="configModal"
          :class="$style.ModifyForm"
          :temporaryData='modalData'>
        </ModifyForm>
      </div>
    </ModalPanel>
    </div>
    <div style="display: none;width:100%;height:100%;margin: auto;" v-has="{action: 'sys:role:list', effect: 'noPremiss'}">
      <NoPremission></NoPremission>
    </div>
  </div>
</template>
<script>
import request from "@/tools/request";
import Panel from "@/components/Panel";
import TableEncapsulation from "@/components/TableEncapsulation";
import TablePager from "@/components/Pager";
import SearchInput from "@/components/SearchInput";
import ModifyForm from "@/components/ModifyForm";
import ModalPanel from "@/components/ModalPanel";
import { objectFilter } from "@/tools/array";
export default {
  name: "role",
  components: {
    TableEncapsulation,
    TablePager,
    Panel,
    SearchInput,
    ModifyForm,
    ModalPanel,
  },
  data() {
    return {
      visible:false,
      modalData:{},
      tableData: [],
      query: {
        page: 1
      },
      pager: {
        use: true,
        total: 0,
        current: 1,
        size: 10,
        showTotal: true
      },
      name: '',
      operation: true,
      state: '',
      name: '',
      editColumn: [
        {
          name: "角色名称",
          prop: "roleName",
          type: "text",
          required: true,
          width: 24
        },
        // {
        //   name: "备注",
        //   prop: "mark",
        //   type: "text",
        //   required: true,
        //   width: 24
        // },
        // {
        //   name: "类型",
        //   prop: "type",
        //   type: "select",
        //   optionList: [
        //     { name: "后台角色", value: 1 },
        //     { name: "代理角色", value: 2 }
        //   ],
        //   required:true,
        //   width:24
        // },
      ],
    };
  },
  created() {
    this.queryData();
  },
  computed: {
    tableConfig () {
      return {
        selection: true,
        columnConfig: [
          {
            label: "编号",
            type: "text",
            prop: "id",
            align: "center",
            width:100
          },
          {
            label: "名称",
            type: "text",
            prop: "roleName",
            align: "center"
          },
          // {
          //   label: "类型",
          //   type: "other",
          //   prop: "type",
          //   align: "center",
          //   fnc: val => val == 1? "后台角色" : "代理角色",
          //   class: val => val == 1 ? "blue-tag" : "refuse"
          // },
          // {
          //   label: "备注",
          //   type: "text",
          //   prop: "mark",
          //   align: "center"
          // },
          this.operation && {
            type: "operation",
            align: "center",
            label: "操作",
            width:300
          }
        ].filter(Boolean)
        }
    }
  },
  methods: {
    async queryData() {
      let current = this.pager.current
      let size = this.pager.size
      // let state = this.state
      // let name = this.name
      let search = objectFilter({current,size})
      const res = await request({
        url: "/manage/role/query",
        method: "POST",
        data: search
      });
      this.tableData = res.data.records
      this.pager.total = res.data.total
      this.pager.current = res.data.current
      // console.log("数据----", res);
    },
    pageChange(val) {
      this.pager.current = val;
      this.queryData();
    },
    changeSearch(key, val) {
      this[key] = val;
      this.pager.current = 1;
    },
    editStore(row){
      let query = {id: row.id, name: row.roleName}
      this.$router.push({ path: "/sys/role/assign",query:query })
    },
    commentDetail(id){
      this.$router.push({ path: "/storeDetail",query:{id:id,type: "toview"} })
    },
    remove(id){
       this.$confirm('确认删除吗', '操作确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then( async () => {
        let del = await request({
        url: `/manage/role/delete/${id}`,
        method: "POST",
      });
      if(del.code !==200) return
        this.$message.success('删除成功')
        if(this.tableData.length===1 && this.pager.current>1)
          this.$set(this.pager,'current', this.pager.current-1)
        this.queryData()
      })
    },
     async onSubmit(){
      this.$refs['configModal'].onSubmit()
    },
    edit(rowData){
      this.modalData = {...rowData}
      this.visible = true
    },
     onCancel(){
      this.visible = false
    },
    async updateData(values){
    let url, msg
    if (values.id){
      url = '/manage/role/add'
      msg = '编辑成功'
    }else {
      url = '/manage/role/add'
      msg = '添加成功'
    }
    const { roleName  } = values
    let res = await request({
      url: url,
      method: 'POST',
      data: { roleName } 
    })
    if (res.code !==200) return
     this.$message.success(msg)
      this.visible = false
      this.queryData()
    },
  }
}
</script>
<style lang='scss' module>
.examine{
    display: flex;
    align-items: center;
    :global{
        div{
            color: #fff;
            padding: 10px;
            margin: 0 10px;
            border-radius: 2px;
            font-size: 14px;
            cursor: pointer;
            min-width: 60px;
            text-align: center;
        }
        .adopt{
                background: #409EFF;
        }
    }
}
.search_box {
  display: flex;
  align-items: center;
  flex-wrap: wrap;

  :global {
    .el-input {
      width: 150px;
    }
    .el-select {
      .el-input {
        width: 90px;
      }
    }
    .el-input__icon {
      line-height: 27px;
    }
    .el-range-separator{
      line-height: 26px;
    }
  }
}

.topSelect {
  background-color: white;
  border-radius: 4px;
  padding: 20px;
  margin-bottom: 20px;
}
</style>